from odoo import api, fields, models
from odoo.exceptions import UserError
import logging
_logger = logging.getLogger(__name__)

class need(models.Model):
    # properties
    _name = 'pjm.need'
    _inherit = ['mail.thread']
    _order = "order_no, id"
    _description = '企画'
    # String fields
    name = fields.Char(string='企画')
    unit_price = fields.Integer(string="単価", default=22500)
    priority = fields.Selection(selection=[
            ('High', '高'),
            ('Middle', '中'),
            ('Low', '低')
        ], string='重要度')
    # tobe = fields.Char(string='期望')
    # way = fields.Selection(selection=[
    #         ('Business', '业务对应'),
    #         ('DevelopMent', '开发对应'),
    #         ('Other', '其他对应')
    #     ], string='对应方式')
    status = fields.Selection(selection=[
            ('Needing', '企画中'),
            ('Planning', '計画中'),
            ('Developing', '対応中'),
            ('Released', '完了')
        ], string='对应状态')
    develop_flag = fields.Selection(selection=[
            ('Labo', 'ラボ'),
            ('Maintenance', '保守'),
            ('project', 'PJ')
        ], string='開発区分')
    confirmer = fields.Many2one("hr.employee",string='提出者')
    confirm_date = fields.Date(string='提出时间')
    developer = fields.Many2many("hr.employee",string='開発者')
    order_no = fields.Integer(string='開発順番',default=99)
    cost_days = fields.Float(string='開発工数')
    work_days = fields.Float(string='実績工数')
    progress = fields.Float(string='進捗',compute='_compute_process', store=True)
    cost_money = fields.Integer(string='金額(円)', compute='_compute_cost_money', store=True)
    task_id = fields.One2many("project.task","need_id",string='タスク')
    description = fields.Html(string="説明")
    start_date = fields.Date(string='開始')
    end_date = fields.Date(string='終了')

    @api.depends('unit_price', 'cost_days')
    def _compute_cost_money(self):
        for record in self:
            record.cost_money = record.unit_price * record.cost_days


    @api.depends('cost_days', 'work_days')
    def _compute_process(self):
        for record in self:
            if record.cost_days == 0:
                record.progress = 0
            else:
                record.progress = record.work_days / record.cost_days * 100




